import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AppBarDemo extends StatelessWidget {
  const AppBarDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return const AppBarPage();
  }
}

class AppBarPage extends StatefulWidget {
  const AppBarPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return AppBarPageState();
  }
}

class AppBarPageState extends State<AppBarPage> {
  late AppBar mAppBar;
  late int mType = 1;

  generateAppBar1(context) {
    return AppBar(
      title: const Text("AppBar Demo1"),
      actions: <Widget>[
        IconButton(
          onPressed: () {
            ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('This is a snackbar')));
          },
          icon: const Icon(Icons.add_alarm),
          tooltip: 'Show Snackbar',
        ),
        IconButton(
          onPressed: () {
            Navigator.push(context, MaterialPageRoute<void>(
              builder: (BuildContext context) {
                return Scaffold(
                  appBar: AppBar(
                    title: const Text('Next page'),
                  ),
                  body: const Center(
                    child: Text(
                      'This is the next page',
                      style: TextStyle(fontSize: 24),
                    ),
                  ),
                );
              },
            ));
          },
          icon: const Icon(Icons.nat_outlined),
          tooltip: "Go to next page",
        ),
      ],
    );
  }

  generateAppBar2(context) {
    return AppBar(
      title: const Text("AppBar Demo2"),
      actions: <Widget>[
        IconButton(
          onPressed: () {
            ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('This is a snackbar')));
          },
          icon: const Icon(Icons.add_alarm),
          tooltip: 'Show Snackbar',
        ),
        IconButton(
          onPressed: () {
            Navigator.push(context, MaterialPageRoute<void>(
              builder: (BuildContext context) {
                return Scaffold(
                  appBar: AppBar(
                    title: const Text('Next page'),
                  ),
                  body: const Center(
                    child: Text(
                      'This is the next page',
                      style: TextStyle(fontSize: 24),
                    ),
                  ),
                );
              },
            ));
          },
          icon: const Icon(Icons.nat_outlined),
          tooltip: "Go to next page",
        ),
      ],
    );
  }

  void changeAppBar(BuildContext context, int type) {
    print('=====type=$type');
    print('=====${type == 1}');
    setState(() {
      mType = type;
      if (type == 1) {
        mAppBar = generateAppBar1(context);
      } else {
        mAppBar = generateAppBar2(context);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    //在页面绘制的时候就需要设置好AppBar
    mAppBar = generateAppBar2(context);
    return Scaffold(
        appBar: mAppBar,
        body: Row(
          children: [
            Text('mType:$mType'),
            Column(
              children: [
                TextButton(
                    child: const Text("AppBar1"),
                    onPressed: () => changeAppBar(context, 1)),
                TextButton(
                    child: const Text("AppBar2"),
                    onPressed: () => changeAppBar(context, 2)),
              ],
            ),
          ],
        ));
  }
}
